@extends("admin.layout.main")

@section("content")
    <section class="content-header">
      <h1>
        分配权限
      </h1>
      <ol class="breadcrumb">
        <li><a href="javascript:void(0)"><i class="fa fa-dashboard"></i> 系统管理</a></li>
        <li class="active"><a href="/admin/users">用户管理</a></li>
        <li class="active">新增用户</li>
      </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <!-- Small boxes (Stat box) -->
        <div class="row">
            <div class="col-lg-12 col-xs-6">
                <div class="box">
                    <!-- /.box-header -->
                    <a class="btn all_check" href="javascript:void(0)"><i class="fa fa-fw fa-plus"></i>全选</a>
                    <div class="box-body">
                        <form action="/admin/roles/permission/{{$role->id}}" method="POST">
                            {{csrf_field()}}
                            <div class="form-group">
                                <table class="table table-bordered">
                                    <tbody>
                                        <tr>
                                            <th style="width: 10px">#</th>
                                            <th>权限规则</th>
                                            <th>权限名称</th>
                                            <th>权限描述</th>
                                            <th>所属</th>
                                        </tr>
                                        @foreach($permissions as $permission)
                                            <tr>
                                                <td><input class="permission_box" type="checkbox" per-id="{{$permission->id}}" name="permissions[]"
                                                    @if($myPermissions->contains($permission))
                                                      checked
                                                    @endif  
                                                    value="{{$permission->id}}">
                                                </td>
                                                <td>{{$permission->name}}</td>
                                                <td>{{$permission->label}}</td>
                                                <td>{{$permission->description}}</td>
                                                <td>{{$pers[$permission->cid]}}</td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                                </div>

                            </div>
                            @if ($errors->has('permission'))
                                <span class="help-block" style="color:#dd4b39">
                                   {{ $errors->first('permission') }}
                                </span>
                            @endif       
                            <div class="box-footer">
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <script type="text/javascript">
        $('.all_check').click(function(){
            $('.permission_box').click();
        });

        // $('.permission_box').click(function(){
        //     var isChecked = $(this).is(":checked");
        //     var pid = $(this).attr('per-id');
        //     $.ajax({
        //       type: "get",
        //       dataType: "json",
        //       url: "/admin/roles/recursion",
        //       data: {pid:pid},
        //       success: function (data) {
        //           if (data.flag) {
        //               window.location.reload();
        //           }
        //       }
        //     });
        // });
    </script>
@endsection